package com.vm.activity;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.Response.Listener;
import com.android.volley.toolbox.JsonArrayRequest;
import com.vm.commons.VMConst;
import com.vm.model.ActiveSoft;
import com.vm.model.Soft;
import com.vm.myview.ViewUtils;
import com.vm.utils.JsonUtils;
import com.vm.utils.MyVolley;
import com.wwtscapp.apk.R;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class NecessaryAppActivity extends Activity {
	protected static final String TAG = null;
	private RequestQueue mQueue;
	ViewUtils viewUtils;
	ImageView imgbtn_search_back;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.necessary_app);
		mQueue = MyVolley.getRequestQueue();
		viewUtils = new ViewUtils(this);
		imgbtn_search_back = (ImageView) findViewById(R.id.imgbtn_search_back);
		imgbtn_search_back.setOnClickListener(btn_backListener);
		initData();
	}

	public void initData() {
		new ArrayList<Soft>();
		String url = VMConst.REQUEST_URL + "getIndApp";
		JsonArrayRequest jsonObjectRequest = new JsonArrayRequest(url,
				new Listener<JSONArray>() {
					@Override
					public void onResponse(JSONArray arg0) {
						// arg0.length();

						for (int i = 0; i < arg0.length(); i++) {
							try {
								JSONObject jsonObject = arg0.getJSONObject(i);
								ActiveSoft activeSoft = JsonUtils.jsonToBean(
										jsonObject, ActiveSoft.class);

								LinearLayout found_ll_1 = (LinearLayout) findViewById(R.id.found_ll_1);
								if (activeSoft != null) {
									found_ll_1.addView(viewUtils.dyAddView(i,
											activeSoft, 1));
								}

							} catch (JSONException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
						}

						// SearchAdapter myAdapter = new SearchAdapter(
						// NecessaryAppActivity.this, listsofts);
						// ListView listView = (ListView)
						// findViewById(R.id.lv_necessary_app);
						// listView.setOnItemClickListener(lv_listener);
						// listView.setAdapter(myAdapter);
						//
						// Log.i(TAG, Integer.toString(listsofts.size()));
						// // arg0.getJSONObject(index);

					}

				}, new Response.ErrorListener() {
					@Override
					public void onErrorResponse(VolleyError error) {
						Log.e(TAG, error.getMessage(), error);
					}
				});

		mQueue.add(jsonObjectRequest);
	}

	public void onBackPressed() {
		super.onBackPressed();
		System.out.println("按下了back键   onBackPressed()");
	}

	OnClickListener btn_backListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			onBackPressed();
		}
	};
}